﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.Controls;
using MicropleSoft.Controls.DialogBox;
using MicropleSoft.WmsApp.Library.ClientProxy;
using MicropleSoft.WmsApp.Library.Security;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.WmsApp.SecurityAuthentication
{
    public partial class FormLogin : XtraForm
    {
        private readonly IServiceFacadeChannel _serviceFacadeChannel = ClientProxyFactory.GetServiceFacadeClient();
        public FormLogin()
        {
            InitializeComponent();

            ////如果主窗口实例已经创建完成，则此窗口不显示在状态栏，否则显示。
            //if (MicropleSoft.Controls. Common.FormMainSingleInstance != null)
            //{
            //    this.ShowInTaskbar = false;
            //}
            //else
            //{
            //    this.ShowInTaskbar = true;
            //}

#if DEBUG
            this.xLookUpEditWh.EditValue = @"10";
            this.xTextEditUserId.Text = @"admin";
            this.xTextEditPassword.Text = @"admin";
#endif

            try
            {
                var list = _serviceFacadeChannel.GetWhConfigList(" and 1=1");
                this.xLookUpEditWh.Properties.DataSource = list;
            }
            catch (Exception exception)
            {
                MicropleSoft.Controls.DialogBox.CustomMessageBox.Show(exception.Message, CustomMessageBox.MsgBoxButtons.Ok,
                                                                      CustomMessageBox.MsgBoxIcons.Error);
            }
        }

        private void CmdLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string result = UserSession.Login(this.xTextEditUserId.Text, this.xTextEditPassword.Text,
                                              (String) this.xLookUpEditWh.EditValue);
                if (UserSession.IsLogOn)
                {

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MicropleSoft.Controls.DialogBox.CustomMessageBox.Show(result, CustomMessageBox.MsgBoxButtons.Ok,
                                                                     CustomMessageBox.MsgBoxIcons.Error);
                }
            }
            catch (Exception exception)
            {
                MicropleSoft.Controls.DialogBox.CustomMessageBox.Show(exception.Message, CustomMessageBox.MsgBoxButtons.Ok,
                                                                      CustomMessageBox.MsgBoxIcons.Error);
            }
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
